const BlogCommentSet = require('../../service/blog/blogCommentSer')
const {log} = require("debug");


// 构建评论树
function generateCommentTree(comments, parentID = null) {
    const commentTree = [];
    for (const comment of comments) {
        if (comment.parentId === parentID) {
            const children = generateCommentTree(comments, comment.id);
            if (children.length > 0) {
                comment.children = children;
            }
            commentTree.push(comment);
        }
    }
    return commentTree;
}


class BlogCommentCon {
    // 添加评论
    async addBlogComment(ctx) {
        const _this = this
        try {
            const jsonData = ctx.request.body
            const reqData = {
                comment_content: jsonData.commentContent,    // 评论内容
                blog_text_id: jsonData.blogTextId,           // 评论所对应的博客的id
                blog_user_id: jsonData.blogUserId,           // 评论人ID
                parent_id: jsonData.parentId || null,// 指向父评论的id
                response_times: 0                    //回复该评论的次数
            }
            // 筛选参选
            const CommentData = {}
            for (const k in reqData) {
                if (reqData[k]) {
                    CommentData[k] = reqData[k]
                }
            }

            const data = await BlogCommentSet.addBlogComment(CommentData)
            if (data.id) {
                if (reqData.parent_id) {
                    await _this.updateResponseTimes(ctx)
                }
                ctx.body = {
                    code: '200'
                }
            } else {
                ctx.body = {
                    code: '999',
                    message: '保存失败'
                }
            }
        } catch (e) {
            ctx.app.emit('error', e.toString(), ctx)
        }
    }

    //修改评论的回复次数
    async updateResponseTimes(ctx) {
        console.log(11111111111111111111111)
        try {
            const id = ctx.request.body.parentId
            //获取当前id数据
            const commentData = await BlogCommentSet.getData(id)
            //修改回复量
            const responseTimes = Number(commentData[0].dataValues.response_times) + 1
            //修改数据
            await BlogCommentSet.updateData({id: id}, {response_times: responseTimes})
        } catch (e) {
            ctx.app.emit('error', e.toString(), ctx)
        }

    }


    //查询评论列表
    async inquireBlogCommentList(ctx) {
        const jsonData = ctx.request.body.SearchData || {}
        const listQuery = ctx.request.body.ListQuery || {page: 1, limit: 10}
        const reqData = {
            user_Name: jsonData.userName || null,    // 用户名称
            blog_title: jsonData.blogTitle || null,           // 文章名称
        }
        try {
            const data = await BlogCommentSet.inquireBlogCommentList(listQuery, reqData)
            if (data.length) {
                return ctx.body = {
                    code: '200',
                    data: {
                        data: data[0],
                        total: data.total
                    }
                }
            } else {
                return ctx.body = {
                    code: '999',
                    message: '查询失败'
                }
            }

        } catch (e) {
            ctx.app.emit('error', e.toString(), ctx)
        }
    }

    // 博客查询评论
    async inquireBlogComment(ctx) {
        const jsonData = ctx.request.body
        const reqData = {
            comment_content: jsonData.commentContent || null,    // 评论内容
            blog_text_id: jsonData.blogTextId || null,           // 评论所对应的博客的id
            blog_user_id: jsonData.blogUserId || null,           // 评论人ID
            parent_id: jsonData.parentId || null,        // 指向父评论的id
        }

        // 筛选参选
        const searchData = {}
        for (const k in reqData) {
            if (reqData[k]) {
                searchData[k] = reqData[k]
            }
        }

        try {
            const data = await BlogCommentSet.inquireBlogComment(searchData)
            if (data.length) {
                const CommentData = generateCommentTree(data[0])
                ctx.body = {
                    code: '200',
                    data: CommentData
                }
            } else {
                ctx.body = {
                    code: '200',
                    message: '暂无数据'
                }
            }
        } catch (e) {
            ctx.app.emit('error', e.toString(), ctx)

        }
    }

    // 删除评论
    async deleteBlogComment(ctx) {
        const id = ctx.request.body.id
        try {
            const data = await BlogCommentSet.deleteBlogComment(id)

            if (data === 1) {
                ctx.body = {
                    code: '200'
                }
            } else {
                ctx.body = {
                    code: '999',
                    message: '删除失败'
                }
            }
        } catch (e) {
            ctx.app.emit('error', e.toString(), ctx)

        }
    }

    //评论页面查询评论接口
    async getBlogCommentList(ctx) {
        try {
            const listQuery = ctx.request.body.listQuery || {page: 1, limit: 10}
            const searchData = {}
            if (ctx.request.body.searchData) {
                ctx.request.body.searchData.userName && (searchData.user_Name = ctx.request.body.searchData.userName)
                ctx.request.body.searchData.creatrdAt && (searchData.creatrdAt = ctx.request.body.searchData.creatrdAt)
                ctx.request.body.searchData.parentId ? (searchData.parent_id = ctx.request.body.searchData.parentId) : searchData.parent_id = null
                ctx.request.body.searchData.blogTextId && (searchData.blog_text_id = ctx.request.body.searchData.blogTextId)
            }
            const data = await BlogCommentSet.inquireBlogComment(listQuery, searchData)
            ctx.body = {
                code: '200',
                data: data
            }
        } catch (e) {
            ctx.app.emit('error', e.toString(), ctx)
        }
    }

}


module.exports = new BlogCommentCon()
